package com.heu.blood.transfusion.controller;


import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.GenerateNumberBySnowFlake;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.transfusion.entity.TransfusionAgreementEntity;
import com.heu.blood.transfusion.entity.TransfusionApplicationEntity;
import com.heu.blood.transfusion.service.TransfusionAgreementService;
import com.heu.blood.transfusion.service.TransfusionApplicationService;
import com.heu.blood.transfusion.vo.TransfusionApplicationAnalysisVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**

 * @author myq
 * @since 2023-07-01
 */
@RestController
@Slf4j
@RequestMapping("transfusion/application")
public class TransfusionApplicationController {
    @Autowired
    private TransfusionApplicationService transfusionApplicationService;
    @Autowired
    private TransfusionAgreementService transfusionAgreementService;



    /**
     * 查询输血申请单信息
     * @param transfusionApplicationEntity
     * @return
     */
    @GetMapping("/query")
    @ControllerLogAnnotation(module = "输血申请单", operate = "查询输血申请单信息")
    public R query(TransfusionApplicationEntity transfusionApplicationEntity){
        List<TransfusionApplicationEntity> transfusionApplications = transfusionApplicationService.query(transfusionApplicationEntity);
        return R.ok().put("data", transfusionApplications);
    }

    /**
     * 根据输血申请单号查询输血申请单信息
     * @param transfusionId
     * @return
     */
    @GetMapping("/queryByTransfusionId")
    @ControllerLogAnnotation(module = "输血申请单", operate = "根据id查询输血申请单信息")
    public R queryByTransfusionId(String transfusionId){
        TransfusionApplicationEntity transfusionApplicationEntity = transfusionApplicationService.queryByTransfusionId(transfusionId);

        return R.ok().put("data", transfusionApplicationEntity);
    }


    /**
     * 分页查询输血申请单信息
     * @param params
     * @return
     */
    @RequestMapping("/list")
    @ControllerLogAnnotation(module = "输血申请单", operate = "分页查询输血申请单信息")
    public R list(@RequestParam Map<String,Object> params){
        PageUtils page=transfusionApplicationService.queryPage(params);

        return R.ok().put("page",page);

    }


    /**
     * 修改
     */
    @PostMapping("/update")
    @ControllerLogAnnotation(module = "输血申请单", operate = "修改输血申请单信息")
    public R update(@RequestBody TransfusionApplicationEntity transfusionApplication){
        transfusionApplicationService.updateById(transfusionApplication);
        return R.ok();
    }

    /**
     * 根据输血单号获取输血同意书信息，提交输血申请
     */
    @ControllerLogAnnotation(module = "输血申请单", operate = "根据输血单号获取输血同意书信息，提交输血申请")
    @PostMapping("/saveFromAgree")
    public R getAgreement(@RequestBody TransfusionApplicationEntity transfusionApplication,@Param("transfusionId") String transfusionId){
        TransfusionAgreementEntity transfusionAgreement = transfusionAgreementService.queryByTransfusionId(transfusionId);
        System.out.println(transfusionAgreement);


        transfusionApplication.setName(transfusionAgreement.getName());
        transfusionApplication.setPatientId(transfusionAgreement.getPatientId());
        transfusionApplication.setTransfusionId(transfusionAgreement.getTransfusionId());
        transfusionApplication.setDepartment(transfusionAgreement.getDepartment());
        transfusionApplication.setWard(transfusionAgreement.getWard());
        transfusionApplication.setBedNumber(transfusionAgreement.getBedNumber());

        String finalSuggestion = transfusionAgreement.getFinalSuggestion();
        if(Objects.equals(finalSuggestion, "同意")){
            transfusionApplication.setIsAgreement("1");
            transfusionApplicationService.save(transfusionApplication);
            return R.ok("提交输血申请单成功");
        }

        return R.ok("患者输血同意书未签字");

    }

    /**
     * 提交输血申请单信息
     */
    @PostMapping("/saveApplication")
    @ControllerLogAnnotation(module = "输血申请单", operate = "提交输血申请单信息")
    public R saveApplication(@RequestBody TransfusionApplicationEntity transfusionApplication,@Param("patientId") String patientId){
        //根据patientId获取输血申请单号（从输血同意书）
//        TransfusionAgreementEntity transfusionAgreement= transfusionAgreementService.queryByPatientId(patientId);
//        transfusionApplication.setTransfusionId(transfusionAgreement.getTransfusionId());
//        transfusionApplication.setPatientId(patientId);
        //输血单号变为在提交输血同意书时生成
        //如果同意书未签字通过则不能提交输血申请
//        String finalSuggestion = transfusionAgreement.getFinalSuggestion();
//        if(Objects.equals(finalSuggestion, "同意")){
//           transfusionApplication.setIsAgreement("1");
//            transfusionApplicationService.save(transfusionApplication);
//            return R.ok("提交输血申请单成功");
//        }
//
//        return R.error("患者输血同意书未签字");
        String flag ="TA";
        GenerateNumberBySnowFlake transfusionId = new GenerateNumberBySnowFlake("transfusionId", flag);
        log.info(transfusionId.genNumberByflag());
        transfusionApplication.setTransfusionId(transfusionId.genNumberByflag());
        transfusionApplicationService.save(transfusionApplication);
        return R.ok("提交输血申请单成功");



    }
    /**
     * 多字段查询
     */
    @GetMapping("/multiQuery")
    @ControllerLogAnnotation(module = "输血申请单", operate = "多字段查询输血申请单信息")
    public R queryMulti(@Param("searchType") String searchType,
                        @Param("beginTime") String beginTime,
                        @Param("endTime") String endTime){
        List<TransfusionApplicationEntity> list = transfusionApplicationService.selectKeyword(searchType,beginTime,endTime);

        return R.ok("查询成功").put("data", list);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    @ControllerLogAnnotation(module = "输血申请单", operate = "批量删除输血申请单信息")
    public R delete(@RequestBody Long[] ids){
        transfusionApplicationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @RequestMapping("/transfusionApplicationAnalysis")
    @ControllerLogAnnotation(module = "输血申请单", operate = "批量删除输血申请单信息")
    public R transfusionApplicationAnalysis(@RequestParam Map<String,Object> params){
        TransfusionApplicationAnalysisVo transfusionApplicationAnalysisVo = transfusionApplicationService.
                TransfusionApplicationAnalysis(params);
        return R.ok().put("data", transfusionApplicationAnalysisVo);
    }




}

